package io.github.wjehovah.yyn.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;

import io.github.wjehovah.yyn.R;

/**
 * 显示网页的界面
 * Created by yingchen on 2015/8/11.
 */
public class WebShowActivity extends Activity {
    //返回
    private TextView mBackTextView;

    //评论
    private Button mCommentButton;

    //加载网页的进度条
    private ProgressBar mLoadingProgressBar;

    //显示网页的WebView
    private WebView mShowWebView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.web_show_activity);

        Intent intent = getIntent();
        String commentNum = intent.getStringExtra("COMMENT");
        String weburl = intent.getStringExtra("WEBURL");
        Log.e("TAG", "comment===" + commentNum + ";weburl" + weburl);

        //初始化控件
        initViews();

        if(!TextUtils.isEmpty(commentNum)){
            //评论按钮显示
            mCommentButton.setText("评论("+commentNum+")");
        }

        //显示网页
        mShowWeb(weburl);

    }


    /**
     * 显示网页
     * @param weburl
     */
    private void mShowWeb(String weburl) {
        //WebView的一些设置
        mShowWebView.getSettings().setJavaScriptEnabled(true); //支持JavaScript

        //显示网页
        mShowWebView.loadUrl(weburl);

        mShowWebView.setWebChromeClient(new WebChromeClient(){
            @Override
            public void onProgressChanged(WebView view, int newProgress) {
                super.onProgressChanged(view, newProgress);
                mLoadingProgressBar.setProgress(newProgress);
                if(mLoadingProgressBar.getProgress() == mLoadingProgressBar.getMax()){
                    mLoadingProgressBar.setVisibility(View.GONE);
                }
            }

        });

    }

    /**
     * 初始化控件
     */
    public void initViews(){
        mBackTextView = (TextView) findViewById(R.id.tv_web_show_back);
        mCommentButton = (Button) findViewById(R.id.btn_web_show_comment);
        mLoadingProgressBar = (ProgressBar) findViewById(R.id.pb_web_show_loading);
        mShowWebView = (WebView) findViewById(R.id.wv_web_show_content);
    }


    /**
     * 返回
     * @param view
     */
    public void back(View view){
        finish();
    }


}
